{extend name="admin/base" /}
{block name="main"}
<!-- page -->
<!--添加按钮和搜索框-->
<!--<div class="row addBtnSearch">
    <div class="col-md-9 col-sm-9 add">
        <a href="{:__URL('ADMIN_MAIN/goods/addgoodsspec')}" class="add_btn"><i class="icon icon-add1"></i> 添加规格</a>
    </div>
</div>-->
<div class="mb-20 flex flex-pack-justify">
    <div class="">
        <a href="{:__URL('ADMIN_MAIN/goods/addgoodsspec')}" class="btn btn-primary J-add"><i class="icon icon-add1"></i> 添加规格</a>
    </div>
</div>

<!--表格-->
<table class="table v-table">
    <thead>
        <tr>
            <th>规格名称</th>
            <th>关联品类</th>
            <th>是否启用</th>
            <th>排序</th>
            <th class="col-md-2 pr-14 operationLeft">操作</th>
        </tr>
    </thead>
    <tbody class="trs" id='list'>
      
    </tbody>
</table>
<div class="page clearfix">
    <div class="M-box3 m-style fr">
    </div>
</div>
<!-- page end -->
{/block}
{block name="script"}
<script type="text/javascript">
require(['utilAdmin','util'], function (utilAdmin,util) { 
    $(function () {
        LoadingInfo(1);
    });
    function setGoodsSpecField(id, name, e) {
        if (name == 'is_visible') {
            var value = $(e).attr("is_visible");
        } else if (name == 'sort') {
            var value = $(e).val();
        }
        $.ajax({
            type: "post",
            url: "{:__URL('ADMIN_MAIN/goods/setgoodsspecfield')}",
            data: {'id': id, 'name': name, 'value': value},
            dataType: "json",
            success: function (data) {
                if (data['code'] > 0) {
                    if (name == 'is_visible') {
                        if (value == 1) {
                            $("#isvisible_" + id).html('是');
                            $("#isvisible_" + id).attr("is_visible", '0');
                        } else {
                            $("#isvisible_" + id).html('否');
                            $("#isvisible_" + id).attr("is_visible", '1');
                        }
                    }
                    LoadingInfo($('#page_index').val());
                    utilAdmin.message(data["message"],"success");
                } else {
                    utilAdmin.message(data["message"], "warning");
                }
            }
        });
    }
//批量删除
    function batchDelete() {
        var spec_ids = new Array();
        $("#list input[type='checkbox']:checked").each(function () {
            if (!isNaN($(this).val())) {
                spec_ids.push($(this).val());
            }
        });
        if (spec_ids.length == 0) {
            utilAdmin.message("请选择需要操作的记录");
            return false;
        }
        delSpec(spec_ids);
    }

//加载数据
    function LoadingInfo(page_index) {
        $('#page_index').val(page_index ? page_index : '1');
        $.ajax({
            type: "post",
            url: "{:__URL('ADMIN_MAIN/goods/goodsspeclist')}",
            data: {"page_index": page_index, "page_size": $("#showNumber").val()},
            success: function (data) {
                var html = '';
                if (data["data"].length > 0) {
                    for (var i = 0; i < data["data"].length; i++) {
                        html += '<tr align="center">';
                        html += '<td>' + data['data'][i]['spec_name'] + '</td>';
                        html += '<td>' + data["data"][i]["attr_value_list"] + '</td>';
                        if (data["data"][i]["is_visible"]) {
                            if (data["data"][i]["is_platform"]) {
                                html += '<td style="width:10%;"><span class="label label-success">是</span></td>';
                            }else{
                                html += '<td style="width:10%;"><a href="javascript:void(0);" class="is_visible label label-success add1" id="isvisible_' + data["data"][i]["spec_id"] + '" is_visible="0" data-id="' + data["data"][i]["spec_id"] + '" data-type="is_visible">是</a></td>';
                            }
                        } else {
                            if (data["data"][i]["is_platform"]) {
                                html += '<td style="width:10%;"><span class="label label-danger">否</span></td>';
                            }else{
                                html += '<td style="width:10%;"><a href="javascript:void(0);" class="is_visible label label-danger add1" id="isvisible_' + data["data"][i]["spec_id"] + '" is_visible="1" data-id="' + data["data"][i]["spec_id"] + '" data-type="is_visible">否</a></td>';
                            }
                        }
                        html += '<td style="width:5%;"><input type="text" class="form-control sort-form-control" data-spec_id="'+ data["data"][i]["spec_id"] +'" value="'+data["data"][i]["sort"]+'"></td>';
                        if (data["data"][i]["is_platform"]) {
                            html += '<td class="fs-0 operationLeft"><a href="' + __URL('ADMIN_MAIN/goods/updategoodsspec?spec_id=' + data["data"][i]["spec_id"]) + '" class="btn-operation">编辑</a></td>';
                        } else {
                            html += '<td class="fs-0 operationLeft"><a href="' + __URL('ADMIN_MAIN/goods/updategoodsspec?spec_id=' + data["data"][i]["spec_id"]) + '" class="btn-operation">编辑</a><a href="javascript:;" data-id="' + data["data"][i]["spec_id"] + '" class="btn-operation text-red1 add2">删除</a></td>';   
                        }
                        html += '</tr>';
                    }
                } else {
                    html += '<tr align="center"><td colspan="5" class="h-200">暂无符合条件的数据记录</td></tr>';
                }
                $("#list").html(html);
                utilAdmin.tips();
                utilAdmin.page(".M-box3", data['total_count'], data["page_count"], page_index, LoadingInfo);
            }
        });
    }

    /* 删除属性 */
    function delSpec(spec_id) {

        utilAdmin.alert("确定要删除吗？",function(){
            $.ajax({
                type: "post",
                url: "{:__URL('ADMIN_MAIN/goods/deletegoodsspec')}",
                data: {'spec_id': spec_id.toString()},
                dataType: "json",
                success: function (data) {
                    if (data['code'] > 0) {
                        LoadingInfo($('#page_index').val());
                        utilAdmin.message(data["message"],"success");
                    } else if (data['code'] == 0) {
                        utilAdmin.message(data["message"], "danger");
                    } else if (data['code'] == -1) {
                        utilAdmin.message('当前规格正在使用中，不能删除！');
                    }
                }
            });
        })

    }

//全选
    function CheckAll(event) {
        var checked = event.checked;
        $(".#list input[type = 'checkbox']").prop("checked", checked);
    }

$('body').on('click','.add1',function(){
    var id=$(this).attr('data-id');
    var type=$(this).attr('data-type');
    var _this=$(this);
    setGoodsSpecField(id,type,_this);
})
$('body').on('click','.add2',function(){
    var id=$(this).attr('data-id');
    delSpec(id);
})
$("body").on('change', '.sort-form-control', function(){
    var spec_id = $(this).data('spec_id');
    var sort_val = $(this).val();
    $.ajax({
        type : "post",
        url : "{:__URL('ADMIN_MAIN/Goods/setgoodsspecfield')}",
        async : true,
        data : {id : spec_id,name: 'sort', value : sort_val},
        success : function(data) {
            if (data["code"] > 0) {
                util.message(data["message"],'success',LoadingInfo($('#pageIndex').val()));
            }else{
                util.message(data["message"],'danger');
            }
        }
    })
})

})
</script>
{/block}